<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container >
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item label="费用申请单号" :labelCol="{span:6}" :wrapperCol="wrapperCol"
                               prop="feeApplicationNum">
              <a-input disabled v-model="model.feeApplicationNum" placeholder="请输入费用申请单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="审批流程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalProcess">
              <j-dict-select-tag
                type="list"
                :disabled="formDisabled"
                v-model="model.approvalProcess"
                dictCode="fee_application_flow"
                placeholder="请选择审批流程"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              label="申请人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="applicant"
            >
              <a-input-search
                v-model="model.applyUserName"
                placeholder="请输入申请人"
                readonly
                enter-button
                @search="handleSelectUser1"
                :disabled="formDisabled"
              ></a-input-search>
              <a-input v-model="model.applicant" hidden></a-input>
            </a-form-model-item>
          </a-col>
          <Tsl-Select-User-Single ref="selectUserModal1" @selectFinished="selectOK"></Tsl-Select-User-Single>

          <a-col :span="8">
            <a-form-model-item label="申请部门" :labelCol="{span:6}" :wrapperCol="wrapperCol"
                               prop="sysOrgCode">
              <a-input v-model="model.applicantDepartment" placeholder="请输入申请部门" disabled></a-input>
              <a-input v-model="model.sysOrgCode" hidden></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="申请日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="applicantDate">
              <j-date placeholder="请选择申请日期" v-model="model.applicantDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="申请金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="applicantAmount">
              <a-input v-model="model.applicantAmount" placeholder="请输入申请金额"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vendorName">
              <j-popup @input="handleCallBack" v-model="model.vendorName" placeholder="请输入供应商名称" code="supplier_main_info" field="vendorName,supplierId" org-fields="supplier_name,id" dest-fields="vendorName,supplierId"></j-popup>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseBank">
              <a-input  v-model="model.purchaseBank"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseAccount">
              <a-input  v-model="model.purchaseAccount"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="申请原因" :labelCol="{span:2}" :wrapperCol="{span:20}" prop="applicantReason">
              <a-textarea v-model="model.applicantReason" rows="5" placeholder="请输入申请原因" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction, putAction } from '@/api/manage'
import TslSelectUserSingle from '@comp/tsl/TslSelectUserSingle.vue'
import { definitionStartByDataId, isFirstInitiator } from '@views/flowable/api/definition'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin'
import JFormContainer from '@comp/jeecg/JFormContainer.vue'

export default {
  name: 'FeeApplicationForm',
  mixins: [JVxeTableModelMixin],
  components: {
    TslSelectUserSingle,
    JFormContainer,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    customFormData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false,
    },
  },
  data() {
    return {
      model: {
        feeApplicationNum:'',
        applicantDepartment:'',
        purchaseBank:'',
        purchaseAccount:'',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        list:'/procurement/feeApplication/list',
        add: '/procurement/feeApplication/add',
        edit: '/procurement/feeApplication/edit',
        queryById: '/procurement/feeApplication/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //如果是审批流进来的时候要将审批流的数据显示出来
    if (this.customFormData.id != null) {
      this.model = this.customFormData
    }
  },
  methods: {
    handleCallBack(str,res){
      console.log("11111111111111111111111111111",res)
      this.model.supplierId = res.supplierId
      this.model.vendorName = res.vendorName
      getAction('/procurement/supplierInformation/queryById', { id: res.supplierId }).then((res) => {
        this.supplier = res.result
        this.model.purchaseBank = this.supplier.bank
        this.model.purchaseAccount = this.supplier.bankCode
      })
    },
    getSequenceNum() {
      let url = '/sys/fillRule/executeRuleByCode/fee_request_code'
      putAction(url, {}).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          this.model.feeApplicationNum = res.result
        }
      })
    },
    add() {
      this.getSequenceNum()
      this.edit(this.model)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      console.log("this.model", this.model)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              if (!this.model.id) {
                this.model.dataId = res.message
                that.applySubmit(this.model)
              }
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    applySubmit(record) {
      if (record.dataId && record.dataId.length < 1) {
        this.$message.error('审批时必须传入参数dataId')
        return
      }

      //对于自定义业务，判断是否是驳回或退回的第一个发起人节点
      isFirstInitiator(record.dataId, record).then((res) => {
        if (res.success && res.result) {
          // 只有第一次能自动申请
          return
        } else {
          var params = Object.assign(
            {
              dataId: record.dataId,
            },
            record
          )
          definitionStartByDataId(record.dataId, 'feeApplicationService', params)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
              } else {
                this.$message.error(res.message)
              }
            })
            .finally()
        }
      })
    },
    handleSelectUser1() {
      this.$refs.selectUserModal1.visible = true
    },
    //防止提交多次
    handleOk() {},
    selectOK(data) {
      this.$set(this.model, 'applyUserName', data.employeeName)  // 使用Vue的响应式更新
      this.$set(this.model, 'applicant', data.employeeCode)
      this.$set(this.model, 'applicantDepartment', data.departCode_dictText)
    },
  }
}
</script>